package androidstore.app.data;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.URI;

import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import androidstore.app.data.AppData;


import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.xmlrpc.android.XMLRPCClient;
import org.xmlrpc.android.XMLRPCException;


public class AppFetcher {
	    private static final String CLASSTAG = AppFetcher.class.getSimpleName();
	    private static final String QBASE = "http://androidstore2011.appspot.com/xmlrpc/";
	    private static final String QURL = "http://androidstore2011.appspot.com";
	    private static final String QMETHOD= "app.GetTopApp";
	    private static final String QTOPAPP = "app.GetTopApp";
	    private static final String QCATAPP = "app.GetbyCategory";
	    private static final String QDOWN = "app.Download";
	    private static final String QGETCOMMENT = "app.GetComment";
	    private static final String QADDCOMMENT = "app.AddComment";
	    private static final String QSearch = "app.Search";
	    
	    private String query;
		private XMLRPCClient client;
		private URI uri;

	    public AppFetcher() {
	        this.query = AppFetcher.QBASE ;
	    }
	    public void Download(Integer appID) {
			uri = URI.create(QBASE);
			client = new XMLRPCClient(uri);
			//Integer rs = 0;
			
			try {
				 final String  result = (String)client.call(QDOWN,appID);
				 //final Integer rs =  Integer.parseInt((String)client.call(QDOWN,appID)); 
				 
			}catch (XMLRPCException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();	
			}
			
	    }
	    public ArrayList<AppData> getData(String name, Object [] params ) {
			uri = URI.create(QBASE);
			client = new XMLRPCClient(uri);
			ArrayList<AppData> Apps = new ArrayList<AppData>();
			try {
				final  String result =  (String)client.call(name,params); 
				DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
				DocumentBuilder db = dbf.newDocumentBuilder();
				Document doc = db.parse( new ByteArrayInputStream(result.getBytes("UTF-8")));
				NodeList nodes_param= doc.getElementsByTagName("param");
	            for (int i = 0; i < nodes_param.getLength(); i++) {
	                  Node paramNode = nodes_param.item(i);
	                  Element paramElement = (Element) paramNode;
	                  NodeList nodes_member = paramElement.getElementsByTagName("member");
	                  AppData App = new AppData();
	                  for(int j = 0; j< nodes_member.getLength(); j++){
	                	  Node memberNode = nodes_member.item(j);
	                	  Element memberElement = (Element) memberNode;
	                	  NodeList node_values = memberElement.getElementsByTagName("value");
	                	  Node valueNode = node_values.item(0);

	                	  if(j == 0){
	                		  App.AppScreenShot = valueNode.getTextContent();
	                	  }
	                	  if (j == 1){
	                		  App.DeviceName = valueNode.getTextContent();
	                	  }
	                	  if (j == 2){
	                		  App.linkIcon = valueNode.getTextContent();
	                	  }		                	  
	                	  if (j == 3){
	                		  App.Description = valueNode.getTextContent();
	                	  }	
	                	  if (j == 4){
	                		  App.KeyWord = valueNode.getTextContent();
	                	  }	     
	                	  if (j == 5){
	                		  App.AppName = valueNode.getTextContent();
	                	  }	                	  
	                	  if (j == 6){
	                		  App.Size = valueNode.getTextContent();
	                		  //App.Price = Float.parseFloat(valueNode.getTextContent());
	                	  }	          
	                	  if (j == 7){
	                		  App.Price = Float.parseFloat(valueNode.getTextContent());
	                	  }	  	                	  
	                	  if (j == 8){
	                		  App.DownloadNumber = Integer.parseInt(valueNode.getTextContent());
	                	  }	
	                	  if (j == 9){
	                		  App.linkSource = valueNode.getTextContent();
	                	  }	
	                	  
	                	  if (j == 10){
	                		  App.ReleaseDate = valueNode.getTextContent();
	                	  }
	                	  if (j == 11){
	                		  App.linkScreenShot = valueNode.getTextContent();
	                	  }	                	  
	                	  if (j == 12){
	                		  App.AppSource = valueNode.getTextContent();
	                	  }		                	  
	                	  if (j == 13){
	                		  App.VersionName = valueNode.getTextContent();
	                	  }     	  
	                	  if (j == 14){
	                		  App.Key = valueNode.getTextContent();
	                	  }               	  
	                	  if (j == 15){
	                		  App.LanguageName = valueNode.getTextContent();
	                	  } 
	                	  if (j == 16){
	                		  App.AppIcon = valueNode.getTextContent();
	                	  }
	                	  if (j== 17)
	                	  {
	                		  App.PaypalAccount = valueNode.getTextContent();
	                	  }      
	                	  if (j == 18){
	                		  App.id = Integer.parseInt(valueNode.getTextContent());
	                	  }
	                	  if (j == 19){
	                		  App.CategoryName = valueNode.getTextContent();
	                	  }	 	                	  
	                  }
	                  Apps.add(App);
	            }	

			} 
			catch (XMLRPCException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();	
			} catch (SAXException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ParserConfigurationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
			return Apps;
	    }

	    public ArrayList<AppData> getTopApp(Integer isFree){
	    	Object [] params = {QURL, isFree};
	    	return getData(QTOPAPP, params);
	    }

	    public ArrayList<AppData> getCategoryApp(String categoryName){
	    	Object [] params = {QURL,categoryName};
	    	return getData(QCATAPP,params);
	    }
	    
	    public ArrayList<AppData> Search(String appname){
	    	Object [] params = {appname,QURL};
	    	return getData(QSearch,params);
	    }

	    public ArrayList<ReviewData> getReview(Integer AppID, Integer isFull){
	    	Object [] params = {AppID,isFull};
			uri = URI.create(QBASE);
			client = new XMLRPCClient(uri);
			ArrayList<ReviewData> Reviews = new ArrayList<ReviewData>();
			try {
				final  String result =  (String)client.call(QGETCOMMENT,params); 
				DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
				DocumentBuilder db = dbf.newDocumentBuilder();
				Document doc = db.parse( new ByteArrayInputStream(result.getBytes("UTF-8")));
				NodeList nodes_param= doc.getElementsByTagName("param");
	            for (int i = 0; i < nodes_param.getLength(); i++) {
	                  Node paramNode = nodes_param.item(i);
	                  Element paramElement = (Element) paramNode;
	                  NodeList nodes_member = paramElement.getElementsByTagName("member");
	                  ReviewData Review = new ReviewData();
	                  for(int j = 0; j< nodes_member.getLength(); j++){
	                	  Node memberNode = nodes_member.item(j);
	                	  Element memberElement = (Element) memberNode;
	                	  NodeList node_values = memberElement.getElementsByTagName("value");
	                	  Node valueNode = node_values.item(0);
	                	  switch(j){
		                	  case 0:
		                		  Review.CommentContent = valueNode.getTextContent();
		                		  break;
		                	  case 2:
		                		  Review.CustomerName = valueNode.getTextContent();
		                		  break;
		                	  case 3:
		                		  Review.DatePost = valueNode.getTextContent();
		                		  break;
		                	  case 1:
		                		  Review.Votes = Integer.parseInt(valueNode.getTextContent());
		                		  break;		                		  
	                	 }
	                  }
	                  Reviews.add(Review);
	            }	

			} 
			catch (XMLRPCException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();	
			} catch (SAXException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ParserConfigurationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
			return Reviews;
	    }

}
